package two.dimensional.arrays;

/**
 * @author shkstart
 * @date 2022/4/17 - 21:54
 */
public class AaaryTest03 {
    public static void main(String[] args) {
//        数组的反转
//        方法一：
        String arr[] = new String[]{"AA", "BB", "CC", "DD", "EE", "FF", "GG"};
//        for (int i = 0; i < arr.length / 2; i++ ) {
//            String temp = "";
//            temp = arr[i];
//            arr[i] = arr[(arr.length - 1) - i ];
//            arr[(arr.length - 1) - i ] = temp;
//        }
//        方法二：
        for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
            String temp = "";
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
//        查找（或搜索）
//        线性查找：
        String dest = "SS";
        boolean flag = true;
        for (int i = 0; i < arr.length; i++) {
            if (dest.equals(arr[i])) {
                System.out.println("找到了指定的元素，位置为：" + i);
                flag = false;
                break;
            }
        }
        if (flag) {
            System.out.println("很遗憾，没有找到！");
        }



//    二分法查找：
//    前提：所以查找的数组必须有序
        int arr1[] = new int[]{-11,-2,-1,0,22,44,55,67,87,99,100};

        int dest1 = 55;
        dest1 = 2389;
        int head = 0;
        int end = arr1.length - 1;
        boolean flag1 = true;
        while(head <= end) {
            int middle = (head + end) / 2;

            if (dest1 == arr1[middle]) {
                System.out.println("找到了指定的元素，位置为：" + middle);
                flag1 = false;
                break;
            } else if (dest1 < arr1[middle]) {
                end = middle - 1;
            } else if (dest1 > arr1[middle]) {
                head = middle + 1;
            }
        }
        if (flag1) {
            System.out.println("很遗憾，没有找到！");
        }
    }


    }
